//
//  CDCollectionViewCell.m
//  SugarPile
//
//  Created by LC on 16/2/29.
//  Copyright © 2016年 www.lanou39.蓝鸥科技. All rights reserved.
//

#import "CDCollectionViewCell.h"
#import "LCPrefixHeader.pch"
@interface CDCollectionViewCell ()
@property (nonatomic, retain)UIView *back;/**<白色背景*/

@property (nonatomic, retain)UILabel *smg;/**<图片下的 主文本*/
//@property (nonatomic, retain)UILabel *selfId;
@property (nonatomic, retain)UIImageView *path;/**<图片(主)*/
@property (nonatomic, retain)UILabel *line1;/**<分割线*/
@property (nonatomic, retain)UIImageView *like;/**<赞*/
@property (nonatomic, retain)UIImageView *reply;/**<评论*/
@property (nonatomic, retain)UIImageView *favorite;/**<收藏*/
@property (nonatomic, retain)UILabel *like_count;/**<赞的人数*/
@property (nonatomic, retain)UILabel *reply_count;/**<评论数*/
@property (nonatomic, retain)UILabel *favorite_count;/**<收藏数*/
@property (nonatomic, retain)UILabel *line;/**<就是一根线*/
@property (nonatomic, retain)UIImageView *avatar;/**<头像*/
@property (nonatomic, retain)UILabel *name;/**<这个图片(主)对应的 名字*/
@property (nonatomic, retain)UILabel *username;/**<头像 对应的用户名*/

@end

@implementation CDCollectionViewCell
- (void)dealloc
{
    [super dealloc];
    [_model release];
    [_smg release];
    [_path release];
    [_like release];
    [_reply release];
    [_favorite release];
    [_like_count release];
    [_reply_count release];
    [_favorite_count release];
    [_line release];
    [_avatar release];
    [_name release];
    [_username release];
}
//初始化
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        //初 背景
        self.back = [[UIView alloc] init];
        _back.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_back];
        [_back release];
        
        //分割线1
        self.line1 = [[UILabel alloc] init];
        _line1.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_line1];
        [_line1 release];
        
        //初 图片
        self.path = [[UIImageView alloc] init];
        _path.backgroundColor = [UIColor whiteColor];
        [self.contentView addSubview:_path];
        [_path release];
        
        //初 文本
        self.smg = [[UILabel alloc] init];
        [self.contentView addSubview:_smg];
        _smg.textColor = [UIColor grayColor];
        _smg.backgroundColor = [UIColor whiteColor];
        [_smg release];
        
        //初 攒的图标
        self.like = [[UIImageView alloc] init];
        _like.image = [UIImage imageNamed:@"zan"];
        [self.contentView addSubview:_like];
        [_like release];
        
        //初 评论图标
        self.reply = [[UIImageView alloc] init];
        _reply.image = [UIImage imageNamed:@"pinglun"];
        [self.contentView addSubview:_reply];
        [_reply release];
        
        //初 收藏图标
        self.favorite = [[UIImageView alloc] init];
        _favorite.image = [UIImage imageNamed:@"shoucang"];
        [self.contentView addSubview:_favorite];
        
        [_favorite release];
        
        //初 攒个数label
        //设置 字体大小 9
        //设置 字体颜色 亮灰
        self.like_count = [[UILabel alloc] init];
        _like_count.font = [UIFont systemFontOfSize:9];
        _like_count.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_like_count];
        [_like_count release];
        
        //初 评论个数
        //设置 字体大小 9
        //设 字体颜色 亮灰
        self.reply_count = [[UILabel alloc] init];
        _reply_count.font = [UIFont systemFontOfSize:9];
        _reply_count.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_reply_count];
        [_reply_count release];
        
        //初 收藏个数
        //设 字体大小 9
        //设 字体颜色 亮灰
        self.favorite_count = [[UILabel alloc] init];
        _favorite_count.font = [UIFont systemFontOfSize:9];
        _favorite_count.textColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_favorite_count];
        [_favorite_count release];
        
        //一条 宽度为 0.3 分割线
        self.line = [[UILabel alloc] init];
        _line.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_line];
        [_line release];
        
        //初 头像
        self.avatar = [[UIImageView alloc] init];
        [self.contentView addSubview:_avatar];
        _avatar.contentMode = UIViewContentModeScaleAspectFill;
        _avatar.clipsToBounds = YES;
        _avatar.userInteractionEnabled = YES;
        UITapGestureRecognizer *avatarTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushYongHuAction:)];
        [_avatar addGestureRecognizer:avatarTap];
        [_avatar release];
        
        //初 用户名
        //设 字体size 11
        //设 字体color lightGray
        self.username = [[UILabel alloc] init];
        _username.font = [UIFont systemFontOfSize:11];
        _username.textColor = [UIColor lightGrayColor];
        _username.userInteractionEnabled = YES;
        UITapGestureRecognizer *userNameTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushAlbumAction:)];
        [_username addGestureRecognizer:userNameTap];
        [self.contentView addSubview:_username];
        [_username release];
        
        //初 用户名
        //设 字大 11
        //设 字色 灰
        self.name = [[UILabel alloc] init];
        _name.font = [UIFont systemFontOfSize:11];
        _name.textColor = [UIColor grayColor];
        _name.userInteractionEnabled = YES;
        UITapGestureRecognizer *nameTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushAlbumAction:)];
        [_name addGestureRecognizer:nameTap];
        [self.contentView addSubview:_name];
        [_name release];
        
    }
    return self;
}

#pragma mark -setModel重写set方法
- (void)setModel:(CDModel *)model
{
    //重写 set 方法
    if (_model != model) {
        [_model release];
        _model = [model retain];
#warning start 因为下方的applyLayoutAttributes:计算的文本和图片的height 不正确 所以我在set方法中重新的计算了文本 图片的高度
        [_path sd_setImageWithURL:[NSURL URLWithString:[model.path stringByReplacingOccurrencesOfString:@"_webp" withString:@""]]];
        _smg.text = model.msg;
        //文本行数 0
        _smg.numberOfLines = 0;
        [_smg sizeToFit];
        //设置文本框的最大范围 为6行
        CGFloat h = [[self class] heightWithText:self.model.msg];
        if (h > 15.51 * 6) {
            h = 15.51 * 6;
        }
        
        //设置图片的 frame
        //高度为 self.contentView.frame.size.width *model.height.doubleValue / model.width.doubleValue
        //对应于 contentView 宽度 下的 contentView高度 = contentView 宽度 * 图片的高度 / 图片的宽度
        _path.frame = CGRectMake(0, 0, self.contentView.frame.size.width, self.contentView.frame.size.width *model.height.doubleValue / model.width.doubleValue);
        
        _line1.frame = CGRectMake(0, _path.frame.size.height, self.contentView.frame.size.width, 0.3);
        
        //设置文本 frame
        //高度为 h 为通过文本自适应高度 计算得出 文本框左右都留有 5 的边界
        _smg.frame = CGRectMake(5, _path.frame.origin.y + _path.frame.size.height + 5, self.contentView.frame.size.width - 10, h);
        
        //文本字大
        _smg.font = [UIFont systemFontOfSize:13];
        
        //设 背景大小 图高 + 字高 + 其他控件高80
#warning back背景用的是 图片,文字 的背景
        _back.frame = CGRectMake(0, 0, self.contentView.frame.size.width, _path.frame.size.height + _smg.frame.size.height + 75);
        //阴影的
        _back.layer.shadowOffset = CGSizeMake(0.1, 0.1);
        //阴影的不透明度
        _back.layer.shadowOpacity = 0.3;
        //阴影的半径
        _back.layer.shadowRadius = 1;
        
        
        //对 评论文本数 赋值
        _reply_count.text = model.reply_count.stringValue;
        
        //设置 评论图片的 frame 大小
        _reply.frame = CGRectMake(5, _path.frame.size.height + _smg.frame.size.height + 10, 10, 10);
        
        //计算 文本宽度
        CGFloat replyHeight = [[self class] widthWithText:self.model.reply_count.stringValue];
        
        //设置 评论数的 frame 尺寸
        //x = 边界预留 5 + 评论图片的width尺寸  + 给它于 评论图片 加上 2 的边界
        //y = 图片 的 高度 + 文本 的 高度 + 10
        //width = 计算出来的 文本宽度
        //height = 10;
        _reply_count.frame = CGRectMake(5 + 10 + 2, _path.frame.size.height + _smg.frame.size.height + 10, replyHeight, 10);
        
        //赞图片 赋值
        _like_count.text = model.like_count.stringValue;
        
        //设置 赞的frame
        //x = 边界预留5 + 评论图片的宽(10) + 评论数的宽(replyHeight) + 评论数 于 评论图片 中间 加上 2 的边界 + 距离评论Frame的 距离 8
        _like.frame = CGRectMake(5 + 10 + replyHeight + 2 + 8, _path.frame.size.height + _smg.frame.size.height + 10, 10, 10);
        CGFloat likeHeight = [[self class] widthWithText:self.model.like_count.stringValue];
        _like_count.frame = CGRectMake(5 + 10 + replyHeight + 2 + 8 + 10 + 2 , _path.frame.size.height + _smg.frame.size.height + 10, likeHeight, 10);
        
        //设置赞图片, 赞的个数
        _favorite_count.text = model.favorite_count.stringValue;
        _favorite.frame = CGRectMake(5 + 10 + replyHeight + 2 + 8 + 2 + 8 + likeHeight + 8 , _path.frame.size.height + _smg.frame.size.height + 10, 10, 10);
        CGFloat f = [[self class] widthWithText:self.model.favorite_count.stringValue];
        _favorite_count.frame = CGRectMake(5 + 10 + replyHeight + 2 + 8 + 2 + 8 + 8 + likeHeight + 2 + 10, _path.frame.size.height + _smg.frame.size.height + 10, f, 10);
        
        // 分割线
        _line.frame = CGRectMake(0, _path.frame.size.height + _smg.frame.size.height + 10 + 10 + 10, _path.frame.size.width, 0.3);
        
        //头像
        _avatar.frame = CGRectMake(5, _line.frame.origin.y + 10, 25, 25);
        [_avatar sd_setImageWithURL:[NSURL URLWithString:model.avatar]];
        _avatar.layer.cornerRadius = _avatar.frame.size.width / 2;
        _avatar.clipsToBounds = YES;
        
        
        //文本的名字
        _name.frame = CGRectMake(5 + _avatar.frame.size.width + 5, _line.frame.origin.y + 10, self.contentView.frame.size.width - _avatar.frame.size.width - 10, 10);
        _name.text = model.name;
        
        //用户名
        _username.frame = CGRectMake(5 + _avatar.frame.size.width + 5, _name.frame.origin.y + 10 + 5, self.contentView.frame.size.width - _avatar.frame.size.width - 10, 10);
        _username.text = [NSString stringWithFormat:@"by %@", model.username];
        
    }
}
//计算文本宽度 自适应宽度时用
+ (CGFloat)widthWithText:(NSString *)text
{
    //保存文本属性
    NSDictionary *dic = @{NSFontAttributeName:[UIFont systemFontOfSize:10]};
    //设置最大 尺寸
    //高度于label一致
    CGSize size = CGSizeMake((WIDTH - 30) / 2 - 10, 10);
    //计算出 出一个 rect
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    //返回得到的宽度
    return rect.size.width;
}

+ (CGFloat)heightWithText:(NSString *)text
{
    // 保存文本属性(大小)
    NSDictionary *dic = @{NSFontAttributeName: [UIFont systemFontOfSize:13]};
    // 预设尺寸 文本最大不超过这个尺寸
    // 根据设置的宽度计算高度(宽度和label保持一致)
    CGSize size = CGSizeMake((WIDTH - 30) / 2 - 10, 500);
    // 根据文本 计算出一个CGRect
    // 参数1: 尺寸范围
    // 参数2: 文本属性
    // 参数3: 按照什么方式获取rect
    // 参数4: nil
    CGRect rect = [text boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:dic context:nil];
    // 返回得到的高度
    
    return rect.size.height;
}

- (void)pushAlbumAction:(UITapGestureRecognizer *)tap
{
    [self.albumDelegate pushAlbum:_model.albumId.stringValue maxCount:_model.albumCount];
}

- (void)pushYongHuAction:(UITapGestureRecognizer *)tap
{
    [self.delegate pushYongHu:_model.senderId.stringValue];
}


- (void)applyLayoutAttributes:(UICollectionViewLayoutAttributes *)layoutAttributes
{
   }


@end
